# -*- coding: utf-8 -*-
#
# Dr. D Studios
# ---------------------------------------------------
#
"""
This file is loaded into conf.py in project docs directory and defines the defaults
for all projects. The conf.py can then override values if required. Remember that
relative path values set here will be relative to the project's conf.py not this file.
"""

import sys, os, re

from rodin.platform import Platform
g_platform = Platform()


miki_project = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
miki_version = os.getenv("MIKI_VER", "v??_??_??")

# General configuration
# ---------------------

# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named "sphinx.ext.*") or your custom ones.
extensions = ["sphinx.ext.autodoc",         # enables autodoc from Python source directives
              "sphinx.ext.intersphinx",     # enables inter sphinx references
              "sphinx.ext.todo",            # allows todos to be hidden on build
              "sphinx.ext.coverage",        # allows generating a documenation coverage report
              "sphinx.ext.pngmath",         # renders math as png images
              "sphinx.ext.graphviz",        # enables graphing directives (using graphviz)
              "sphinx.ext.inheritance_diagram",  # enables inheritance diagram directives
              "sphinx.ext.ifconfig",             # allows some sections to be omitted based on config variables
              "breathe"                          # doxygen bridge
              
              #"rst2pdf.pdfbuilder",        # At present the pdf builder breaks the standard code-block directive
                                            # so it is added in on a need only basis by the builder.
              ]

# Now add the local Miki extensions
sys.path.insert(0, os.path.join(miki_project, 'sources'))
extensions.append("miki.extensions.external_reference") # store external urls in config and link to them using keywords
extensions.append("miki.extensions.autolisting")        # generate listing automatically of code entries

# Add any paths that contain templates here, relative to this directory.
templates_path = [os.path.join(miki_project, "resources", "sphinx", "templates")]

# The suffix of source filenames.
source_suffix = ".rst"

# The encoding of source files.
#source_encoding = "utf-8"

# The master toctree document.
master_doc = "index"

# General information about the project.
project = "Unknown"
project_safe_name = "unknown"
project_display_name = "Unknown"
copyright = u"2010, Dr.D Studios"

# The version info for the project you"re documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.

# The full version, including alpha/beta/rc tags.
release = "?_?_?"

# The short X.Y version.
version = "?_?"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ""
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = "%B %d, %Y"

# List of documents that shouldn"t be included in the build.
#unused_docs = []

# List of directories, relative to source directory, that shouldn"t be searched
# for source files.
exclude_trees = ["static"]

# The reST default role (used for this markup: `text`) to use for all documents.
#default_role = None

# If true, "()" will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = False

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False

# the default highlight language
highlight_language = "python"

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = "sphinx"

# A list of ignored prefixes names for module index sorting.
modindex_common_prefix = [""]

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {"http://docs.python.org/dev": None}

# Show todo notes
todo_include_todos = True

# A string of reStructuredText that will be included at the end of every source file that is read. 
# This is the right place to add substitutions that should be available in every file. 
rst_epilog = """
.. |project| replace:: %s
""" % project_display_name


# Options for Autodoc extension
# ------------------------

# Use both __init__ and class docstring to autoclassing
autoclass_content = "both"


# Options for Breath extension
# ------------------------
breathe_projects = {
}

breathe_default_project = None


# Options for Graphviz extension
# ------------------------


# Options for Inheritance Diagrams extension
# ------------------------
inheritance_graph_attrs = dict(rankdir="TB")
inheritance_node_attrs = dict()


# Options for Miki extras extension
# ------------------------
external_links = {}


# Options for HTML output
# -----------------------

# The style sheet to use for HTML and HTML Help pages. A file of that name
# must exist either in Sphinx" static/ path, or in one of the custom paths
# given in html_static_path.
html_style = "default.css"

# The name for this set of Sphinx documents.  If None, it defaults to
# "<project> v<release> documentation".
#html_title = None

# A shorter title for the navigation bar.  Default is the same as html_title.
#html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = "./_static/logo.png"

# The name of an image file (within the static path) to use as favicon of the
# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["static"]

# If not "", a "Last updated on:" timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = "%b %d, %Y"

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {
#}

# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {
#}

# If false, no module index is generated.
#html_use_modindex = True

# If false, no index is generated.
#html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False

# If true, the reST sources are included in the HTML build as _sources/<name>.
html_copy_source = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ""

# If nonempty, this is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = ""

# Output file base name for HTML help builder.
htmlhelp_basename = "%s_doc" % project_safe_name

# Some additional variables to pass to templates for context
html_context = {"miki_version":miki_version}

# Options for LaTeX output
# ------------------------

latex_elements = {
    "papersize":"a4paper",
    "pointsize":"10pt"
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, document class [howto/manual]).
_stdauthor = ur"Dr. D Studios"
latex_documents = [
  ("user/index", "%s_user.tex" % project_safe_name, ur"%s User Documentation" % project_display_name, _stdauthor, "manual"),
  ("technical/index", "%s_technical.tex" % project_safe_name, ur"%s Technical Documentation" % project_display_name, _stdauthor, "manual"),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False

# Additional stuff for the LaTeX preamble.
#latex_preamble = ""

# Documents to append as an appendix to all manuals.
#latex_appendices = []

# If false, no module index is generated.
latex_use_modindex = False


# Options for PDF output
# ------------------------

# Grouping the document tree into PDF files. List of tuples
# (source start file, target name, title, author).
_stdauthor = ur"Dr. D Studios"
pdf_documents = [
  ("user/index", "%s_user" % project_safe_name, ur"%s User Documentation" % project_display_name, _stdauthor),
  ("technical/index", "%s_technical" % project_safe_name, ur"%s Technical Documentation" % project_display_name, _stdauthor),
]

# A comma-separated list of custom stylesheets. Example:
pdf_stylesheets = ['sphinx','kerning','a4']
if g_platform.is_mac:
    pdf_stylesheets.remove('kerning') # kerning causes problems on osx due to missing fonts

# Create a compressed PDF
# Use True/False or 1/0
#pdf_compressed=False

# A colon-separated list of folders to search for fonts. Example:
# pdf_font_path=['/usr/share/fonts', '/usr/share/texmf-dist/fonts/']

# Language to be used for hyphenation support
#pdf_language="en_EN"

# If false, no index is generated.
#pdf_use_index = True

# If false, no modindex is generated.
#pdf_use_modindex = True

# If false, no coverpage is generated.
#pdf_use_coverpage = True

    
# Utility function
# ---------------------
def compute_derived_values(**source_values):
    """
    Some values can be derived from others. This function will attempt to return derived values from the given source
    values. 
    
    :kwargs source_values: each keyword argument represents a source value that if not None will cause 
        appropriate derivatives to be calculated.
    :returns: dictionary of derived values
    
    .. note::
    
        This function will be called once automatically by Miki and copies its local settings into the module namespace.
        It can also be called again manually if source values have changed and the user wants to regenerate derived 
        values::
        
            # auto set globals to include newly calculated derived values
            globals.update(compute_derived_values(project=project))
            
    """
    derived = {}
    
    project = source_values.get("project", None)
    if project:
        project_display_name = " ".join(map(lambda w: w[0].upper()+w[1:], re.split("[ _]+", project)))
        project_safe_name = project_display_name.replace(" ", "_").lower()

        # A string of reStructuredText that will be included at the end of every source file that is read. 
        # This is the right place to add substitutions that should be available in every file. 
        rst_epilog = """\n.. |project| replace:: %s\n""" % project_display_name

        # Options for Breath extension
        # ------------------------
        breathe_projects = {
            project: os.path.abspath("./_build/doxygen/xml")
        }
        
        breathe_default_project = project
    
    
        # Options for HTML output
        # -----------------------
    
        # Output file base name for HTML help builder.
        htmlhelp_basename = "%s_doc" % project_safe_name
    
        
        # Options for LaTeX output
        # ------------------------
        
        # Grouping the document tree into LaTeX files. List of tuples
        # (source start file, target name, title, author, document class [howto/manual]).
        _stdauthor = ur"Dr. D Studios"
        latex_documents = [
          ("user/index", "%s_user.tex" % project_safe_name, ur"%s User Documentation" % project_display_name, _stdauthor, "manual"),
          ("technical/index", "%s_technical.tex" % project_safe_name, ur"%s Technical Documentation" % project_display_name, _stdauthor, "manual"),
        ]
            
            
        # Options for PDF output
        # ------------------------
        
        # Grouping the document tree into PDF files. List of tuples
        # (source start file, target name, title, author).
        _stdauthor = ur"Dr. D Studios"
        pdf_documents = [
          ("user/index", "%s_user" % project_safe_name, ur"%s User Documentation" % project_display_name, _stdauthor),
          ("technical/index", "%s_technical" % project_safe_name, ur"%s Technical Documentation" % project_display_name, _stdauthor),
        ]
        
        # update derived dictionary, note we keep source values in their as well
        l = locals()
        l.pop("derived", None)
        derived.update(l)
    
    release = source_values.get("release", None)
    if release:
        derived["version"] = release.rsplit("_", 1)[0]
    
    return derived

# Copyright 2008-2012 Dr D Studios Pty Limited (ACN 127 184 954) (Dr. D Studios)
#
# This file is part of anim-studio-tools.
#
# anim-studio-tools is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# anim-studio-tools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with anim-studio-tools.  If not, see <http://www.gnu.org/licenses/>.

